var flag = true; //标记位

function FocusItem(obj) {
    if ($(obj).attr('name') == 'verycode') {
        $(obj).next().next().html('').remove('error');
    }else{
        $(obj).next('span').html('').remove('error');
    }
}
function CheckItem(obj) {
    var msgBox =$(obj).next('span');
    switch ($(obj).attr('name')){
        case "username":
            if (obj.value == ""){
                msgBox.html('用户名不能为空');
                msgBox.addClass('error');
                flag = false;
            }else {
                var u = /^[A-Za-z\u4e00-\u9fa5]+$/u;
                 var url="http://localhost:8080/lyq/shangc/usernamecheck?name="+encodeURI($(obj).val())+"&"+new Date().getTime();
                 $.get(url, function (data) {
                    if (data == "false"){
                        msgBox.html('用户名已存在');
                        msgBox.addClass('error');
                        flag = false;
                    }else if (!u.test(obj.value)){
                        msgBox.html('用户名格式不正确');
                        msgBox.addClass('error');
                        flag = false;
                    } else {
                        msgBox.html().removeClass('error');
                        flag = true;
                    }
                 });
            }
            break;
        case  "name":
            var han = /^[\u4e00-\u9fa5]+$/;
            if (obj.value == ""){
                msgBox.html('用户姓名不能为空');
                msgBox.addClass('error');
                flag = false;
            }else if (!han.test(obj.value)){
                msgBox.html('只能输入汉字');
                msgBox.addClass('error');
                flag = false;
            } else { flag = true;}
            break;
        case "password":
            var pwd = /^[0-9a-zA-Z]{6,10}$/;
            if (obj.value == ""){
                msgBox.html('密码不能为空');
                msgBox.addClass('error');
                flag = false;
            }else if(!pwd.test(obj.value)){
                msgBox.html('密码格式不正确');
                msgBox.addClass('error');
                flag = false;
            }else{ flag = true;}
            break;
        case "pass":
            if (obj.value == ""){
                msgBox.html('确认密码不能为空');
                msgBox.addClass('error');
                flag = false;
            }else  if ($(obj).val() != $('input[name="password"]').val()) {
                msgBox.html('两次密码输入不一致');
                msgBox.addClass('error');
                flag = false;
            }else { flag = true;}
            break;
        case "mobile":
            var tele = /^1[34567890]{1}\d{9}$/;
            if (obj.value == ""){
                msgBox.html('手机号码不能为空');
                msgBox.addClass('error');
                flag = false;
            }else if(!tele.test(obj.value)){
                msgBox.html('手机号码格式不正确');
                msgBox.addClass('error');
                flag = false;
            } else { flag = true;}
            break;
        case "address":
            if (obj.value == ""){
                msgBox.html('收货地址不能为空');
                msgBox.addClass('error');
                flag = false;
            }else { flag = true;}
            break;
        case "verycode":
            var numshow = $(obj).next().next();
            if (obj.value == ""){
                numshow.html('验证码不能为空');
                numshow.addClass('error');
                flag = false;
            }else{
                var url = "http://localhost:8080/lyq/admin/checkusernum?num="+encodeURI($(obj).val())+"&"+new Date().getTime();
                $.get(url, function (nume) {
                    if (nume == 'false'){
                        numshow.html('验证码不正确');
                        numshow.addClass('error');
                        flag = false;
                    } else{
                        numshow.html().removeClass('error');
                        flag = true;
                    }
                })
            }
            break;
        case "email":
            if (obj.value !="") {
                var em =/^\w+@[0-9a-z]+\.[a-z]+$/;
                if (!em.test(obj.value)){
                    msgBox.html('邮箱格式不正确');
                    msgBox.addClass('error');
                    flag = false;
                }else { flag = true;}
            }
            break;
    }
}

function checkForm(frm) {
    var els = frm.getElementsByTagName('input');
    //有onblur属性的才需要验证
    for (var i = 0;i<els.length;i++){
        if (els[i]!=null){
            if (els[i].getAttribute("onblur")){
                CheckItem(els[i]);
            }
        }
    }
    return flag;
}